package coffeenc.mall.admin.service.impl;

import coffeenc.mall.admin.config.AdminUserDetails;
import coffeenc.mall.admin.exception.Asserts;
import coffeenc.mall.admin.mapper.UmsAdminMapper;
import coffeenc.mall.admin.mapper.UmsResourceMapper;
import coffeenc.mall.admin.dto.UmsAdminRegisterParam;
import coffeenc.mall.admin.entity.UmsAdmin;
import coffeenc.mall.admin.entity.UmsResource;
import coffeenc.mall.common.jwt.JwtTokenUtil;
import coffeenc.mall.admin.service.UmsAdminCacheService;
import coffeenc.mall.admin.service.UmsAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class UmsAdminServiceImpl implements UmsAdminService{
    @Autowired
    private PasswordEncoder encoder;
    @Autowired
    private UmsAdminMapper adminMapper;
    @Autowired
    private UmsResourceMapper resourceMapper;
    @Autowired
    private UmsAdminCacheService umsAdminCacheService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    UserDetailsService userDetailsService;
    /**
     * 用户注册
     */
    @Override
    public UmsAdmin register(UmsAdminRegisterParam umsAdminRegisterParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminRegisterParam, umsAdmin);
        umsAdmin.setCreateTime(LocalDateTime.now());
        umsAdmin.setStatus(1);
        /**
         * 查询数据库中是否有同名用户
         */
        UmsAdmin umsAdminExample = new UmsAdmin();
        umsAdminExample.setUsername(umsAdmin.getUsername());
        List<UmsAdmin> umsAdminList = adminMapper.selectByCondition(umsAdminExample);
        if (umsAdminList.size()>0)
            return null;
        /**
         * 密码明文加密形成密文，并将用户信息写入数据库
         */
        String encodePassword = encoder.encode(umsAdmin.getPassword());
        umsAdmin.setPassword(encodePassword);
        adminMapper.insert(umsAdmin);
        return umsAdmin;
    }

    /**
     * 用户登录
     */
    @Override
    public String login(String username, String password) {
        String token = null;
        try {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            if (!encoder.matches(password,userDetails.getPassword())){
                Asserts.fail("密码不正确");
            }
            if (!userDetails.isEnabled()){
                Asserts.fail("账号已被锁定");
            }
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);   /* 将认证后的用户信息添加到SecurityContextHolder中 */
            return jwtTokenUtil.generateToken(userDetails);    /* 生成token并返回 */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }
}
